<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Article extends Controller
{
    protected $middleware = [
        'CheckAdmin'
    ];

    public function lists(Request $request)
    {
        if ($request->isAjax()) {
            $title = $request->get('title', '', 'trim');
            $category_id = $request->get('category_id', 0, 'intval');
            $page = $request->get('page',1,'intval');
            $page = $page > 0 ? $page : 1;
            $limit = $request->get('limit',10,'intval');
            $article = Db('article')->alias('a');
            if ($title !== '') {
                $article->where('a.title', 'like', '%' . $title . '%');
            }
            if ($category_id !== 0) {
                $article->where('a.category_id', $category_id);
            }
            $count = $article->count();
            $list = $article
                ->join('category ct', 'a.category_id = ct.category_id', 'LEFT')
                ->field('a.article_id,a.title,a.image,a.start_time,a.create_time,ct.name as category_name')
                ->page($page,$limit)
                ->order('a.sort desc')
                ->select();
            return ['code' => 0, 'data' => $list, 'count' => $count];
        } else {
            return $this->fetch();
        }
    }

    public function allLists()
    {
        $list = Db('article')
            ->order('article_id desc')
            ->select();
        return ['code' => 0, 'data' => $list];
    }

    public function addArticle(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $data['category_id'] = $request->post('category_id', 0, 'intval');
            $data['title'] = $request->post('title', '', 'trim');
            $data['image'] = $request->post('article_image', '', 'trim');
            $data['description'] = $request->post('description', '', 'trim');
            $data['content'] = $request->post('content', '', 'trim');
            $data['start_time'] = $request->post('start_time', '', 'trim');
            $data['teacher_id'] = $request->post('teacher_id', 0, 'intval');
            $data['sort'] = $request->post('sort', 0, 'intval');
            $data['create_time'] = date('Y-m-d H:i:s');
            if ($data['category_id'] == 0) {
                return ['code' => 1, 'msg' => '请选择分类'];
            }
            if ($data['title'] == '') {
                return ['code' => 1, 'msg' => '请填写文章名称'];
            }
            if (false === Db('article')->insert($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $category = Db('category')->order('sort asc')->select();
            $this->assign('category', $category);
            $teacher = Db('teacher')->where('type',1)->order('sort asc')->select();
            $this->assign('teacher', $teacher);
            return $this->fetch();
        }
    }

    public function editArticle(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $article_id = $request->post('article_id', 0, 'intval');
            $data['category_id'] = $request->post('category_id', 0, 'intval');
            $data['title'] = $request->post('title', '', 'trim');
            $data['image'] = $request->post('article_image', '', 'trim');
            $data['description'] = $request->post('description', '', 'trim');
            $data['content'] = $request->post('content', '', 'trim');
            $data['start_time'] = $request->post('start_time', '', 'trim');
            $data['teacher_id'] = $request->post('teacher_id', 0, 'intval');
            $data['sort'] = $request->post('sort', 0, 'intval');
            if ($data['category_id'] == 0) {
                return ['code' => 1, 'msg' => '请选择分类'];
            }
            if ($data['title'] == '') {
                return ['code' => 1, 'msg' => '请填写文章名称'];
            }
            if (false === Db('article')->where('article_id',$article_id)->update($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $article_id = $request->get('id', 0, 'intval');
            $info = Db('article')->where('article_id',$article_id)->find();
            $this->assign('info', $info);
            $category = Db('category')->order('sort asc')->select();
            $this->assign('category', $category);
            $teacher = Db('teacher')->where('type',1)->order('sort asc')->select();
            $this->assign('teacher', $teacher);
            return $this->fetch();
        }
    }

    public function deleteArticle(Request $request)
    {
        $article_id = $request->get('id', 0, 'intval');
        if (false === Db('article')->where('article_id', $article_id)->delete()) {
            return ['code' => 1, 'msg' => '操作失败'];
        } else {
            return ['code' => 0, 'msg' => '操作成功'];
        }
    }
}